package com.java.mall_imooc.controller;

import com.java.mall_imooc.common.utils.R;
import com.java.mall_imooc.form.CartAddForm;
import com.java.mall_imooc.form.CartUpdateForm;
import com.java.mall_imooc.interceoter.LoginInterceptor;
import com.java.mall_imooc.service.CartService;
import com.java.mall_imooc.vo.CartVo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 购物车
 *
 * @author jiangli
 * @since 2020-03-16 21:10
 */
@RestController
@RequestMapping("/carts")
@AllArgsConstructor
public class CartController {
    private CartService cartService;

    /**
     * 查询购物车
     */
    @GetMapping
    public R<CartVo> list() {
        return cartService.list(LoginInterceptor.getUserInfo().getUserId());
    }

    /**
     * 新增
     */
    @PostMapping
    public R<CartVo> add(@RequestBody @Valid CartAddForm addForm) {
        return cartService.add(addForm, LoginInterceptor.getUserInfo().getUserId());
    }

    /**
     * 更新购物车
     */
    @PutMapping("{productId}")
    public R<CartVo> update(@PathVariable("productId") Integer productId, @RequestBody CartUpdateForm cartUpdateForm) {
        return cartService.update(LoginInterceptor.getUserInfo().getUserId(), productId, cartUpdateForm);
    }

    /**
     * 删除
     */
    @DeleteMapping("{productId}")
    public R<CartVo> delete(@PathVariable("productId") Integer productId) {
        return cartService.delete(LoginInterceptor.getUserInfo().getUserId(), productId);
    }

    /**
     * 全选
     */
    @PutMapping("/selectAll")
    public R<CartVo> selectAll(){
        return cartService.selectAll(LoginInterceptor.getUserInfo().getUserId());
    }

    /**
     * 全不选
     */
    @PutMapping("/unSelectAll")
    public R<CartVo> unSelectAll(){
        return cartService.unSelectAll(LoginInterceptor.getUserInfo().getUserId());
    }

    /**
     * 购物车商品总数量
     */
    @GetMapping("/sum")
    public R<Integer> sum(){
        return cartService.sum(LoginInterceptor.getUserInfo().getUserId());
    }
}
